package com.kmproject.haifu.mvp.presenter;

import android.app.Activity;
import android.app.Application;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.Observable;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.jess.arms.widget.CustomPopupWindow;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.hfinterface.HouseReasonRemovalCallBack;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.SecondHandInfoShowContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.api.service.UserService;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.beans.SecondHandInfoBean;
import com.kmproject.haifu.mvp.model.entity.resbean.SecondHandIds;
import com.kmproject.haifu.mvp.ui.adapter.PopuwindowReasonRemovalAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 09/02/2019 09:56
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class SecondHandInfoShowPresenter extends BasePresenter<SecondHandInfoShowContract.Model, SecondHandInfoShowContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public SecondHandInfoShowPresenter(SecondHandInfoShowContract.Model model, SecondHandInfoShowContract.View rootView) {
        super(model, rootView);
    }
    /**
     * 下架提示
     */
    public  void houseReasonRemoval(Activity activity, View mParentView, List<HousePayWayBean> housePayWayBeans, HouseReasonRemovalCallBack houseReasonRemovalCallBack) {
        CustomPopupWindow customPopupWindow = new CustomPopupWindow.Builder().parentView(mParentView).contentView(CustomPopupWindow.inflateView(activity, R.layout.popuwindow_reason_removal))
                .animationStyle(R.style.mypopwindow_anim_style).customListener(new CustomPopupWindow.CustomPopupWindowListener() {
                    @Override
                    public void initPopupView(View contentView) {

                    }
                }).isWrap(true).isHaveBackGround(true).isShowBottom(true).build();
        View view1 = customPopupWindow.getContentView();
        RecyclerView popuwindow_reason_removal_RecyclerView = view1.findViewById(R.id.popuwindow_reason_removal_RecyclerView);
        TextView popuwindow_reason_removal__cancel_txt = view1.findViewById(R.id.popuwindow_reason_removal__cancel_txt);
        popuwindow_reason_removal_RecyclerView.setLayoutManager(new LinearLayoutManager(activity));
        PopuwindowReasonRemovalAdapter adapter=new PopuwindowReasonRemovalAdapter(housePayWayBeans);
        popuwindow_reason_removal_RecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                houseReasonRemovalCallBack.callBack(housePayWayBeans.get(position));
                customPopupWindow.dismiss();
            }
        });

        popuwindow_reason_removal__cancel_txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customPopupWindow.dismiss();
            }
        });
        customPopupWindow.show();
    }
    /**
     * 查询
     *
     * @param secondhandId
     */
    public void secondHandLookOne(String secondhandId) {
        mModel.secondHandLookOne(Api.SECONDHAND_PUBLISH_PUBLISH_LOOK_ONE + secondhandId)
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<SecondHandInfoBean>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<SecondHandInfoBean> secondHandInfoBeanResultBean) {
                        if (AppUtil.isSuccess(secondHandInfoBeanResultBean)) {
                            mRootView.secondHandLookOneSuccess(secondHandInfoBeanResultBean.getData());
                        } else {
                            mRootView.showMessage(secondHandInfoBeanResultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 下架
     * @param lowerShelfReason
     * @param secondHandId
     */
    public void lowerShelfSecondHand(String lowerShelfReason, String secondHandId) {
     mModel.lowerShelfSecondHand(lowerShelfReason,secondHandId).compose(RxUtils.applySchedulers(mRootView))
             .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                 @Override
                 public void onNext(ResultBean resultBean) {
                     if (AppUtil.isSuccess(resultBean))
                     {
                         mRootView.lowerShelfSecondHandSuccess();
                     }else {
                         mRootView.showMessage(resultBean.getMessage());
                     }
                 }
             });
    }


    /**
     * 收藏
     * @param secondHandId
     */
    public void collestSecondHand(String secondHandId) {
       mModel.collestSecondHand(Api.SECONDHAND_COLLECT+secondHandId).compose(RxUtils.applySchedulers(mRootView))
               .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                   @Override
                    public void onNext(ResultBean resultBean) {
                      if (AppUtil.isSuccess(resultBean))
                      {
                          mRootView.collestSecondHandSuccess();
                      }else {
                          mRootView.showMessage(resultBean.getMessage());
                      }
                     }
               });
    }


    /**
     * 取消收藏
     * @param id
     */
    public void  cancelCollestSecondHand(String id) {
        List<String> ids=new ArrayList<>();
        ids.add(id);
        SecondHandIds secondHandIds=new SecondHandIds();
        secondHandIds.setSecondHandIds(ids);
       mModel.cancelCollestSecondHand(AppUtil.entityToMap(secondHandIds)).compose(RxUtils.applySchedulers(mRootView))
               .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                   @Override
                   public void onNext(ResultBean resultBean) {
                       if (AppUtil.isSuccess(resultBean))
                       {
                           mRootView.cancelCollestSecondHandSuccess();
                       }else {
                           mRootView.showMessage(resultBean.getMessage());
                       }
                   }
               });
    }


    /**
     * 删除房源
     * @param secondHandId
     */
    public void remoredSencond(String secondHandId) {
       mModel.remoredSencond(secondHandId).compose(RxUtils.applySchedulers(mRootView))
               .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                   @Override
                   public void onNext(ResultBean resultBean) {
                     if (AppUtil.isSuccess(resultBean))
                     {
                         mRootView.remoredSencondSuccess();
                     }else {
                         mRootView.showMessage(resultBean.getMessage());
                     }
                   }
               });
    }

    /**
     * 获取下架提示语
     * @param value
     * @param category
     */
    public void  dictList(String value, String category) {
      mModel.dictList(value,category).compose(RxUtils.applySchedulers(mRootView))
              .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                  @Override
                  public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                    if (AppUtil.isSuccess(listResultBean))
                    {
                        mRootView.dictListSuccess(listResultBean.getData());
                    }else {
                        mRootView.showMessage(listResultBean.getMessage());
                    }
                  }
              });

    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
